// ===============================================
// error_utils
// 
// Create by Will on 2020/10/5 6:09 PM
// Copyright @data_food.All rights reserved.
// ===============================================

import 'dart:io';
import 'package:flutter_base/core/http/dio_error.dart';
import 'package:flutter_base/core/exception/api_exception.dart';
import 'package:dio/dio.dart';

class ErrorUtils {
  static String messageFrom(Object error) {
    String message;

    switch (error.runtimeType) {
      case SocketException:
        message = '网络异常，请检查您的网络连接';
        break;
      case DioError:
        final dioError = error as DioError;
        final originalError = dioError.error;
        if (originalError == null) {
          message = dioError.toDisplayText();
        } else {
          message = messageFrom(originalError);
        }
        break;
      case CoreKitAPIException:
        final apiException = error as CoreKitAPIException;
        message = apiException.message;
        break;
      default:
        message = error.toString();
        break;
    }

    return message ?? '';
  }
}
